// pages/my/gotBack.js
const app = getApp()
import rsa from "../../utils/jsencrypt.js"
var encrypt = rsa.JSEncrypt
var timeInterval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    code: '',
    realCode: '',
    btnText: '获取验证码',
    currentTime: 60,
    isClick: false,
    errorText: '手机号不可为空'
  },

  /**
   * 生命周期函数--监听页面加载
   */

  changeName(e) {
    console.log(e)
    this.setData({
      code: e.detail.value
    })
  },
  changeTel: function (e) {
    let that = this
    let reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    let phone = e.detail.value;
    this.setData({
      phone,
    })
    if (!that.data.phone) {
      that.setData({
        errorText: '手机号不可为空'
      })
    } else if (!reg.test(that.data.phone)) {
      that.setData({
        errorText: '手机号码格式不正确'
      })
    } else {
      that.setData({
        errorText: ''
      })
    }
  },
  changeInfo() {
    let that = this
    if( this.data.errorText ){
      wx.showToast({
        title: this.data.errorText,
        icon: 'none'
      })
      return
    }
    if(  this.data.code == '' ){
      wx.showToast({
        title: '验证码不可为空',
        icon: 'none'
      })
      return 
    }
    if( this.data.realCode != this.data.code ){
      wx.showToast({
        title: '验证码输入不正确',
        icon: 'none'
      })
      return
    }
    app.get(`dealer/get_dealer_by_phone?Phone=${this.data.phone}`, res => {
      console.log(res)
      if (!res.result) {
        wx.showToast({
          title: '未找到该账号信息，请确认后重试！',
          icon: 'none'
        })
        return
      } else {
        let postData = {
          CustId: app.globalData.uid,
          OpenId: app.globalData.openid,
          ChangeCustId: res.result.CustId
        }
        if(postData.CustId == postData.ChangeCustId){
          wx.showToast({
            title: '没有可找回的账号',
            icon: 'none'
          })
          return
        }
        app.postForm('user/change_t_cust_openid', postData, resp => {
          if (resp.code == 0) {
            app.postForm('user/login_by_openid', {
              OpenId: app.globalData.openid
            }, response => {
              console.log(response)
              app.globalData.user = ret.result
              app.globalData.openid = ret.openid;
              app.globalData.uid = ret.result.Id;
              wx.showToast({
                title: '修改成功',
                icon: 'success',
                duration: 1500,
                success: () => {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1 //返回上一级页面
                    })
                  }, 1500)
                }
              })
            })
          } else {
            wx.showToast({
              title: '修改失败',
            })
          }
        })
      }
    })
  },
  sendMsg() {
    //第一步当然是验证手机号的合法性了，这里就不在累赘
    if (this.data.errorText) {
      wx.showToast({
        title: this.data.errorText,
        icon: 'none'
      })
      return
    }
    //第二步获取验证码
    let that = this;
    let Tel = that.data.phone;
    var currentTime = that.data.currentTime;
    Tel = encrypt.prototype.encrypt(Tel)
    console.log(Tel)
    app.postForm('user/get_phonecode', {
      Tel,
    }, res => {
      console.log(res)
      that.setData({
        isClick: true,
        btnText: currentTime + '秒后获取',
        realCode: res.smscode
      })
      timeInterval = setInterval(function () {
        currentTime--; //倒计时
        that.setData({
          btnText: currentTime + '秒后获取'
        })
        if (currentTime <= 0) {
          clearInterval(timeInterval) //重置倒计时
          that.setData({
            btnText: '获取验证码',
            currentTime: 60,
            isClick: false
          })
        }
      }, 1000);
    })
  },
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.get('user/get_publickey', res => {
      encrypt.prototype.setPublicKey(res.result)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})